// pages/paper/paperOrder/paperOrder.js
let api = require("../../../api/paperApi.js")
let commonApi = require("../../../api/api")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    paper:{},
    showBuy: false,
    isBuy: false,
    userFavPaper: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.paperId)
    let paperId = options.paperId
    let that = this;
    api.getPaperDetial(paperId).then(res=>{
      wx.hideLoading()
      that.setData({ paper: res.data })
      console.log(that.data.paper)

      api.userHasPaper(paperId).then(res=>{
        that.setData({ isBuy: res.data.hasPaper })
      })

      that.setData({ 'userFavPaper': wx.getStorageSync("userFavPaper") })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  immBuy: function() {
    this.setData({
      showBuy: true,
    })
  },
  closeBuy: function() {
    this.setData({
      showBuy: false,
    })
  },

  singleBuy:function(){
    let userInfo = wx.getStorageSync('userInfo')
    let paper_id = this.data.paper.id
    api.singlePay(userInfo.mobile, paper_id).then(res=>{
      wx.hideLoading()
      let orderInfo = res.data
      console.log(orderInfo)
      commonApi.startPay(orderInfo)
    })
  },

  toPaperDetial:function(){
    let that = this
    console.log(that.data.paper)
    wx.navigateTo({
      url: '/pages/paper/paperDetial/paperDetial?paperId='+that.data.paper.id,
    })
  },

  paperFav:function(){
    let that = this;
    console.log(this.data.paper)
    // 判断是否已经收藏 若收藏 则 移除
    api.paperFavChoice(this.data.paper)
    that.setData({ 'userFavPaper': wx.getStorageSync("userFavPaper") })
  }

})